import request from '@/utils/request'

/**
 * 用户登录
 * @param {Object} data - 登录数据
 * @param {string} data.username - 用户名
 * @param {string} data.password - 密码
 * @returns {Promise} 登录结果
 */
export const login = async (data) => {
  try {
    const response = await request.post('/auth/login', data)
    return response.data
  } catch (error) {
    throw error
  }
}

/**
 * 用户登出
 * @returns {Promise} 登出结果
 */
export const logout = async () => {
  try {
    const response = await request.post('/auth/logout')
    return response.data
  } catch (error) {
    throw error
  }
}

/**
 * 获取用户信息
 * @returns {Promise} 用户信息
 */
export const getUserInfo = async () => {
  try {
    const response = await request.get('/auth/user')
    return response.data
  } catch (error) {
    throw error
  }
}

/**
 * 刷新token
 * @returns {Promise} 新的token
 */
export const refreshToken = async () => {
  try {
    const response = await request.post('/auth/refresh')
    return response.data
  } catch (error) {
    throw error
  }
}

/**
 * 修改密码
 * @param {Object} data - 密码数据
 * @param {string} data.oldPassword - 旧密码
 * @param {string} data.newPassword - 新密码
 * @returns {Promise} 修改结果
 */
export const changePassword = async (data) => {
  try {
    const response = await request.put('/auth/password', data)
    return response.data
  } catch (error) {
    throw error
  }
}